/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package visao;
import bean.Usuario;
import java.util.logging.Level;
import java.util.logging.Logger;
import negocio.GerenciarDB;

/**
 *
 * @author Leandro
 */
public class JanelaCadastrarUsuario extends javax.swing.JFrame {

    /**
     * Creates new form JanelaCadastrarUsuario
     */
    public JanelaCadastrarUsuario() {
        initComponents();
        CPFDependente.setEditable(true);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        GrupoTipoUsuario = new javax.swing.ButtonGroup();
        GrupoSexo = new javax.swing.ButtonGroup();
        PainelCadastroUsuario = new javax.swing.JPanel();
        PainelPessoal = new javax.swing.JPanel();
        Nome = new javax.swing.JLabel();
        NomeUsuario = new javax.swing.JTextField();
        CPF = new javax.swing.JLabel();
        CPFUsuario = new javax.swing.JFormattedTextField();
        Nacionalidade = new javax.swing.JLabel();
        NacionalidadeUsuario = new javax.swing.JTextField();
        EmailLabel = new javax.swing.JLabel();
        Email = new javax.swing.JTextField();
        DataNasc = new javax.swing.JLabel();
        DataNascUsuario = new javax.swing.JFormattedTextField();
        try{  
            javax.swing.text.MaskFormatter data= new javax.swing.text.MaskFormatter("##/##/####");  
            DataNascUsuario = new javax.swing.JFormattedTextField(data);  
        }  
        catch (Exception e){  
        }
        erroNome = new javax.swing.JLabel();
        CPFErro = new javax.swing.JLabel();
        ErroEmail = new javax.swing.JLabel();
        erroNasc = new javax.swing.JLabel();
        ErroNac = new javax.swing.JLabel();
        PainelEndereco = new javax.swing.JPanel();
        Endereco = new javax.swing.JLabel();
        EnderecoUsuario = new javax.swing.JTextField();
        Telefone = new javax.swing.JLabel();
        TelUsuario = new javax.swing.JFormattedTextField();
        erroEnd = new javax.swing.JLabel();
        erroTel = new javax.swing.JLabel();
        PainelTipo = new javax.swing.JPanel();
        TipodeUsuario = new javax.swing.JLabel();
        Dependente = new javax.swing.JRadioButton();
        Titular = new javax.swing.JRadioButton();
        CPFdoDependente = new javax.swing.JLabel();
        CPFDependente = new javax.swing.JFormattedTextField();
        ErroTipoUsuario = new javax.swing.JLabel();
        erroCpfTit = new javax.swing.JLabel();
        CancelarCadastroUsuario = new javax.swing.JButton();
        LimparCadastroUsuario = new javax.swing.JButton();
        CadastrarUsuario = new javax.swing.JButton();
        todosCampos = new javax.swing.JLabel();
        iCadastro = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastrar Usuário");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        PainelCadastroUsuario.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        PainelPessoal.setBorder(javax.swing.BorderFactory.createTitledBorder("Informações Pessoais"));
        PainelPessoal.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        Nome.setText("Nome:");
        PainelPessoal.add(Nome, new org.netbeans.lib.awtextra.AbsoluteConstraints(21, 40, 40, -1));

        NomeUsuario.setToolTipText("Nome");
        PainelPessoal.add(NomeUsuario, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 33, 470, -1));

        CPF.setText("CPF:");
        PainelPessoal.add(CPF, new org.netbeans.lib.awtextra.AbsoluteConstraints(23, 70, 30, -1));

        CPFUsuario.setToolTipText("CPF do Usuário");
        try{  
            javax.swing.text.MaskFormatter data= new javax.swing.text.MaskFormatter("###.###.###-##");  
            CPFUsuario = new javax.swing.JFormattedTextField(data);  
        }  
        catch (Exception e){  
        }
        PainelPessoal.add(CPFUsuario, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 70, 130, -1));

        Nacionalidade.setText("Nacionalidade:");
        PainelPessoal.add(Nacionalidade, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 70, -1, -1));

        NacionalidadeUsuario.setToolTipText("Nacionalidade");
        PainelPessoal.add(NacionalidadeUsuario, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 70, 220, -1));

        EmailLabel.setText("Email:");
        PainelPessoal.add(EmailLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 110, 60, -1));
        PainelPessoal.add(Email, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 110, 260, -1));

        DataNasc.setText("Data de Nascimento:");
        PainelPessoal.add(DataNasc, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 110, -1, -1));

        DataNascUsuario.setToolTipText("Data de Nascimento");
        PainelPessoal.add(DataNascUsuario, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 110, 77, -1));

        erroNome.setForeground(new java.awt.Color(255, 0, 0));
        PainelPessoal.add(erroNome, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 30, 17, 31));

        CPFErro.setForeground(new java.awt.Color(255, 0, 0));
        PainelPessoal.add(CPFErro, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 70, 16, 20));

        ErroEmail.setForeground(new java.awt.Color(255, 0, 0));
        PainelPessoal.add(ErroEmail, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 110, 14, 20));
        PainelPessoal.add(erroNasc, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 110, 15, 20));
        PainelPessoal.add(ErroNac, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 70, 17, 23));

        PainelCadastroUsuario.add(PainelPessoal, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, 660, 150));

        PainelEndereco.setBorder(javax.swing.BorderFactory.createTitledBorder("Informações de Endereço"));
        PainelEndereco.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        Endereco.setText("Endereço:");
        PainelEndereco.add(Endereco, new org.netbeans.lib.awtextra.AbsoluteConstraints(18, 36, -1, -1));

        EnderecoUsuario.setToolTipText("Digite Endereço");
        PainelEndereco.add(EnderecoUsuario, new org.netbeans.lib.awtextra.AbsoluteConstraints(92, 33, 462, -1));

        Telefone.setText("Telefone:");
        PainelEndereco.add(Telefone, new org.netbeans.lib.awtextra.AbsoluteConstraints(18, 67, -1, -1));

        TelUsuario.setToolTipText("Telefone");
        try{  
            javax.swing.text.MaskFormatter data= new javax.swing.text.MaskFormatter("(##)####-####");  
            TelUsuario = new javax.swing.JFormattedTextField(data);  
        }  
        catch (Exception e){  
        }
        PainelEndereco.add(TelUsuario, new org.netbeans.lib.awtextra.AbsoluteConstraints(92, 64, 118, -1));
        PainelEndereco.add(erroEnd, new org.netbeans.lib.awtextra.AbsoluteConstraints(73, 33, 15, 20));
        PainelEndereco.add(erroTel, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 64, 10, 20));

        PainelCadastroUsuario.add(PainelEndereco, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 180, 660, 110));

        PainelTipo.setBorder(javax.swing.BorderFactory.createTitledBorder("Informações de Tipo"));
        PainelTipo.setPreferredSize(new java.awt.Dimension(740, 203));
        PainelTipo.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        TipodeUsuario.setText("Tipo de Usuário:");
        PainelTipo.add(TipodeUsuario, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 30, -1, -1));

        GrupoTipoUsuario.add(Dependente);
        Dependente.setSelected(true);
        Dependente.setText("Dependente");
        Dependente.setActionCommand("DEPENDENTE");
        Dependente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DependenteActionPerformed(evt);
            }
        });
        PainelTipo.add(Dependente, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 30, -1, -1));

        GrupoTipoUsuario.add(Titular);
        Titular.setText("Titular");
        Titular.setToolTipText("");
        Titular.setActionCommand("TITULAR");
        Titular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TitularActionPerformed(evt);
            }
        });
        PainelTipo.add(Titular, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 30, -1, -1));

        CPFdoDependente.setText("CPF Titular:");
        PainelTipo.add(CPFdoDependente, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 60, -1, -1));

        CPFDependente.setEditable(false);
        CPFDependente.setToolTipText("CPF do Titular");
        try{  
            javax.swing.text.MaskFormatter data= new javax.swing.text.MaskFormatter("###.###.###-##");  
            CPFDependente = new javax.swing.JFormattedTextField(data);  
        }  
        catch (Exception e){  
        }
        CPFDependente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CPFDependenteActionPerformed(evt);
            }
        });
        PainelTipo.add(CPFDependente, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 60, 107, -1));

        ErroTipoUsuario.setForeground(new java.awt.Color(255, 0, 0));
        PainelTipo.add(ErroTipoUsuario, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 30, 20, 15));
        PainelTipo.add(erroCpfTit, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 50, 16, 30));

        PainelCadastroUsuario.add(PainelTipo, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 300, 660, 110));

        CancelarCadastroUsuario.setText("Cancelar");
        CancelarCadastroUsuario.setToolTipText("Cancelar Operação");
        CancelarCadastroUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelarCadastroUsuarioActionPerformed(evt);
            }
        });
        PainelCadastroUsuario.add(CancelarCadastroUsuario, new org.netbeans.lib.awtextra.AbsoluteConstraints(570, 420, 100, 30));

        LimparCadastroUsuario.setText("Limpar");
        LimparCadastroUsuario.setToolTipText("Limpar Campos");
        LimparCadastroUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LimparCadastroUsuarioActionPerformed(evt);
            }
        });
        PainelCadastroUsuario.add(LimparCadastroUsuario, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 420, 100, 30));

        CadastrarUsuario.setText("Cadastrar");
        CadastrarUsuario.setToolTipText("Cadastrar Usuário");
        CadastrarUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CadastrarUsuarioActionPerformed(evt);
            }
        });
        PainelCadastroUsuario.add(CadastrarUsuario, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 420, 100, 30));

        todosCampos.setForeground(new java.awt.Color(255, 0, 0));
        PainelCadastroUsuario.add(todosCampos, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 420, 270, 30));

        getContentPane().add(PainelCadastroUsuario, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 0, 710, 480));

        iCadastro.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        iCadastro.setText("iCadastro");
        getContentPane().add(iCadastro, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 10, -1, -1));

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-728)/2, (screenSize.height-514)/2, 728, 514);
    }// </editor-fold>//GEN-END:initComponents

    private void CancelarCadastroUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelarCadastroUsuarioActionPerformed
        // TODO add your handling code here:
        this.dispose();
        JanelaInicioAdmin frame = new JanelaInicioAdmin();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }//GEN-LAST:event_CancelarCadastroUsuarioActionPerformed

    private void LimparCadastroUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LimparCadastroUsuarioActionPerformed
        // TODO add your handling code here:
        NomeUsuario.setText("");
        CPFUsuario.setText("");
        DataNascUsuario.setText("");
        NacionalidadeUsuario.setText("");
        Email.setText("");
        EnderecoUsuario.setText("");
        TelUsuario.setText("");
        CPFDependente.setText("");
        Dependente.setSelected(true);
        CPFDependente.setEditable(true);
        CPFDependente.setEnabled(true);
        CPFdoDependente.setForeground(new java.awt.Color(0, 0, 0));
        ErroEmail.setText("");
        ErroNac.setText("");
        ErroTipoUsuario.setText("");
        CPFErro.setText("");
        erroCpfTit.setText("");
        erroEnd.setText("");
        erroNasc.setText("");
        erroNome.setText("");
        erroTel.setText("");
    }//GEN-LAST:event_LimparCadastroUsuarioActionPerformed

    private void CadastrarUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CadastrarUsuarioActionPerformed
        // TODO add your handling code here:
        String nomeUsuario = NomeUsuario.getText();
        String cpfUsuario = CPFUsuario.getText().replaceAll("[.]", "").replaceAll("-", "");
        String dataNasc = DataNascUsuario.getText().replaceAll("/", "");
        String nacionalidadeUsuario = NacionalidadeUsuario.getText();
        String email = Email.getText();
        String enderecoUsuario = EnderecoUsuario.getText();
        String telUsuario = TelUsuario.getText().replaceAll("-", "").replaceAll("[(]", "").replaceAll("[)]", "");
        String cpfDependente;

        String tipoUsuario = GrupoTipoUsuario.getSelection().getActionCommand();
        
        if ("TITULAR".equals(tipoUsuario)) {
            cpfDependente = null;
        } else {
            cpfDependente = CPFDependente.getText();
            cpfDependente = cpfDependente.replaceAll("[.]", "");
            cpfDependente = cpfDependente.replaceAll("-", "");
    
        }

        if ((nomeUsuario.isEmpty()) || ("           ".equals(cpfDependente)) || ("           ".equals(cpfUsuario)) || (cpfUsuario.isEmpty()) || (email.isEmpty()) || (dataNasc.isEmpty())
                || (enderecoUsuario.isEmpty()) || (nacionalidadeUsuario.isEmpty())
                || (telUsuario.isEmpty()) || ("        ".equals(dataNasc)) || ("          ".equals(telUsuario)) || ("          ".equals(telUsuario)) || (tipoUsuario.isEmpty())) {
            todosCampos.setText("* Todos os campos devem ser preenchidos");

            if (nomeUsuario.isEmpty()) {
                erroNome.setText("*");
            } else if (cpfUsuario.length()<3) {
                CPFErro.setText("*");
            } else if (email.isEmpty()) {
                ErroEmail.setText("*");
            } else if (enderecoUsuario.isEmpty()) {
                erroEnd.setText("*");
            } else if (dataNasc.isEmpty()) {
                erroNasc.setText("*");
            } else if (nacionalidadeUsuario.isEmpty()) {
                ErroNac.setText("*");
            } else if (telUsuario.isEmpty()) {
                erroTel.setText("*");
            } else if (tipoUsuario.isEmpty()) {
                ErroTipoUsuario.setText("*");
            }
        } else {
            try {
                 GerenciarDB usuario = new GerenciarDB();
                 Usuario user = new Usuario (nomeUsuario, cpfUsuario, tipoUsuario, email, enderecoUsuario, dataNasc, nacionalidadeUsuario, cpfDependente, telUsuario);
                 usuario.addUsuario(user);
                 JanelaInicioAdmin frame = new JanelaInicioAdmin();
                 frame.setLocationRelativeTo(null);
                 frame.setVisible(true);
                 this.dispose();

            } catch (Exception ex) {
                Logger.getLogger(JanelaCadastrarUsuario.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }//GEN-LAST:event_CadastrarUsuarioActionPerformed

    private void DependenteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DependenteActionPerformed
        // TODO add your handling code here:
        CPFDependente.setEditable(true);
        CPFDependente.setEnabled(true);
        CPFdoDependente.setForeground(new java.awt.Color(0, 0, 0));
    }//GEN-LAST:event_DependenteActionPerformed

    private void TitularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TitularActionPerformed
        // TODO add your handling code here:
        CPFDependente.setEditable(false);
        CPFDependente.setEnabled(false);
        CPFdoDependente.setForeground(new java.awt.Color(155, 155, 155));
        
    }//GEN-LAST:event_TitularActionPerformed

    private void CPFDependenteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CPFDependenteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_CPFDependenteActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JanelaCadastrarUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JanelaCadastrarUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JanelaCadastrarUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JanelaCadastrarUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JanelaCadastrarUsuario().setVisible(true);
            }
              
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel CPF;
    private javax.swing.JFormattedTextField CPFDependente;
    private javax.swing.JLabel CPFErro;
    private javax.swing.JFormattedTextField CPFUsuario;
    private javax.swing.JLabel CPFdoDependente;
    private javax.swing.JButton CadastrarUsuario;
    private javax.swing.JButton CancelarCadastroUsuario;
    private javax.swing.JLabel DataNasc;
    private javax.swing.JFormattedTextField DataNascUsuario;
    private javax.swing.JRadioButton Dependente;
    private javax.swing.JTextField Email;
    private javax.swing.JLabel EmailLabel;
    private javax.swing.JLabel Endereco;
    private javax.swing.JTextField EnderecoUsuario;
    private javax.swing.JLabel ErroEmail;
    private javax.swing.JLabel ErroNac;
    private javax.swing.JLabel ErroTipoUsuario;
    private javax.swing.ButtonGroup GrupoSexo;
    private javax.swing.ButtonGroup GrupoTipoUsuario;
    private javax.swing.JButton LimparCadastroUsuario;
    private javax.swing.JLabel Nacionalidade;
    private javax.swing.JTextField NacionalidadeUsuario;
    private javax.swing.JLabel Nome;
    private javax.swing.JTextField NomeUsuario;
    private javax.swing.JPanel PainelCadastroUsuario;
    private javax.swing.JPanel PainelEndereco;
    private javax.swing.JPanel PainelPessoal;
    private javax.swing.JPanel PainelTipo;
    private javax.swing.JFormattedTextField TelUsuario;
    private javax.swing.JLabel Telefone;
    private javax.swing.JLabel TipodeUsuario;
    private javax.swing.JRadioButton Titular;
    private javax.swing.JLabel erroCpfTit;
    private javax.swing.JLabel erroEnd;
    private javax.swing.JLabel erroNasc;
    private javax.swing.JLabel erroNome;
    private javax.swing.JLabel erroTel;
    private javax.swing.JLabel iCadastro;
    private javax.swing.JLabel todosCampos;
    // End of variables declaration//GEN-END:variables

}
